package jp.ne.iforce.android.wifioffload;

import java.util.regex.Pattern;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.telephony.SmsManager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import static jp.ne.iforce.android.wifioffload.BarCodeActivity.TAG;

public class SmsActivity extends Activity implements Runnable, OnClickListener {
	private ProgressDialog progressDialog;

	private EditText editPhoneno; // 送信先番号
	private TextView editText; // 送信内容
	private Button send_button; // 送信ボタン
	private Button call_button; // 送信ボタン

	private static String send_text;
	private static String dl_pass;

	private String destinationAddress;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sms);

		// 画面を明るい状態に保つ
		Window window = getWindow();
		window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

		Intent i = getIntent();
		send_text = i.getStringExtra("SEND_TEXT");
		dl_pass = i.getStringExtra("DL_PASS");

		// 送信先入力エディタ
		editPhoneno = (EditText) findViewById(R.id.editPhoneno);
		// 送信内容
		editText = (TextView) findViewById(R.id.editText);
		editText.setText(send_text);

		// 送信ボタン
		send_button = (Button) findViewById(R.id.send_button);
		// リスナー登録
		send_button.setOnClickListener(this);

		/**
		 * // Callテストボタン call_button = (Button) findViewById(R.id.call_button);
		 * // リスナー登録 call_button.setOnClickListener(this);
		 **/

	}

	public void onClick(View arg0) {

		// 送信先番号
		destinationAddress = editPhoneno.getText().toString();
		if (destinationAddress != null && destinationAddress.length() == 11) {
			destinationAddress = destinationAddress.trim();

			if (Pattern.matches("090[\\d]{4}?[\\d]{4}?", destinationAddress)
					|| Pattern.matches("080[\\d]{4}?[\\d]{4}?",destinationAddress)) {
				switch (arg0.getId()) {

				case R.id.send_button:
					// SMS通信中ダイアログを表示させる。
					progressDialog = new ProgressDialog(this);
					progressDialog.setTitle("通信中");
					progressDialog.setMessage("SMS送信中・・・");
					progressDialog.setIndeterminate(false);
					progressDialog
							.setProgressStyle(ProgressDialog.STYLE_SPINNER);
					progressDialog.show();
					Thread thread = new Thread(this);
					thread.start();
					break;

				/**
				 * case R.id.call_button: Intent intent = new
				 * Intent(Intent.ACTION_CALL,Uri.parse("tel:" +
				 * destinationAddress)); startActivity(intent); break;
				 **/
				}

			} else {
				Toast.makeText(this, "正しい送信先番号を入力してください", Toast.LENGTH_LONG)
						.show();
			}
		} else {
			Toast.makeText(this, "正しい送信先番号を入力してください", Toast.LENGTH_LONG).show();
		}
	}

	public void run() {
		// SMS
		SmsManager smsManager = SmsManager.getDefault();
		// 送信するテキストを設定
		String text = editText.getText().toString();
		// 送信
		smsManager.sendTextMessage(destinationAddress, null, text, null, null);

		try {
			Thread.sleep(4000);
		} catch (InterruptedException e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		}

		handler.sendEmptyMessage(0);
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			Intent i = new Intent(getApplicationContext(), PasswdActivity.class);
			i.putExtra("DL_PASS", dl_pass);
			startActivity(i);

			progressDialog.dismiss();
			finish();
		}
	};
}